import React from 'react';
import routes from './routes';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import TextInput from 'Shared/components/TextInput';
import TagSelect from 'Shared/components/TagSelect';
import {PERMISSION} from './constants';
import {FORM_OPTIONS, FORM_ROW_OPTIONS} from '../constants';
import {hasPermissions} from './utils';
import {injectIntl, language, formatMessage} from './intl';
import WrappedSelect from '../common/WrappedSelect';
import {goodWillStatus, goodWillType} from '../Enum';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onChange(name, value);
    }

    handleFilterPressEnter = (value, name) => {
        this.props.onChange(name, value);
        this.props.querySearch();
    }
    render() {
        const {condition = {}, brands = [], isDealerUser, querySearch,
            loading, onSelect, onClickReset, permissions} = this.props;
        const dealerInfo = condition.dealerId ? {
            id: condition.dealerId,
            code: condition.dealerCode,
            name: condition.dealerName
        } : null;
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <Form.Item
                                label={formatMessage({
                                    id: 'queryPanel.form.brand',
                                    defaultMessage: '品牌'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="brandId"
                                    value={condition.brandId}
                                    options={brands}
                                    onChange={this.handleFilterChange}/>
                            </Form.Item>
                        </Col>
                        {!isDealerUser &&
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.dealerId',
                                        defaultMessage: '总代'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <DealerSelectPanel
                                        value={dealerInfo}
                                        onSelect={onSelect}/>
                                </Form.Item>
                            </Col>
                        }
                        <Col {...FORM_OPTIONS.col}>
                            <Form.Item label={formatMessage({
                                id: 'queryPanel.form.code',
                                defaultMessage: '编号'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="code"
                                    value={condition.code}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </Form.Item>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <Form.Item label={formatMessage({
                                id: 'queryPanel.form.VIN',
                                defaultMessage: 'VIN'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="vin"
                                    value={condition.vin}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </Form.Item>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <Form.Item label={formatMessage({
                                id: 'queryPanel.form.productCategoryCode',
                                defaultMessage: '车型'
                            })}{...FORM_OPTIONS.item}>
                                <TextInput
                                    name="productCategoryCode"
                                    value={condition.productCategoryCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </Form.Item>
                        </Col>
                    </Row>
                    {isDealerUser && <Row>
                        <Col>
                            <Form.Item label={formatMessage({
                                id: 'queryPanel.form.type',
                                defaultMessage: '归类'
                            })} {...FORM_ROW_OPTIONS.item}>
                                <TagSelect
                                    name="type"
                                    value={condition.type}
                                    options={goodWillType.toList(language)}
                                    onChange={this.handleFilterChange} />
                            </Form.Item>
                        </Col>
                    </Row>
                    }
                    <Row>
                        <Col>
                            <Form.Item label={formatMessage({
                                id: 'queryPanel.form.status',
                                defaultMessage: '状态'
                            })} {...FORM_ROW_OPTIONS.item}>
                                <TagSelect
                                    name="status"
                                    value={condition.status}
                                    options={goodWillStatus.toList(language)}
                                    onChange={this.handleFilterChange} />
                            </Form.Item>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={6}>
                            <Button key="query" type="primary"
                                onClick={querySearch} loading={loading}>
                                {formatMessage({
                                    id: 'queryPanel.btn.search',
                                    defaultMessage: '查询'
                                })}</Button>
                            <Button key="clear" onClick={onClickReset}>{formatMessage({
                                id: 'queryPanel.btn.reset',
                                defaultMessage: '重置'
                            })}</Button>
                        </Col>
                        {<Col span={18} className="col-align-right">
                            {hasPermissions(permissions, PERMISSION.add) && <Button key="add" type="primary" href={`#${routes.add.url()}`}>
                                {formatMessage({
                                    id: 'queryPanel.btn.add',
                                    defaultMessage: '新增'
                                })}</Button>}
                        </Col>}
                    </Row>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    brands: PropTypes.array.isRequired,
    condition: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onChange: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    isDealerUser: PropTypes.bool,
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    querySearch,
    resetQueryPanel,
    selectDealerQuery
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const EMPTY_ARRAY = [];
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getBrands = createSelector(
    state => state.getIn(['page', 'domainData', 'initData', 'brands']),
    data => {
        const brands = data.toJS();
        return brands
            ? brands.map(item => ({
                value: item.id,
                text: `(${item.code})${item.name}`
            }))
            : EMPTY_ARRAY;
    }
);

const mapStateToProps = state => ({
    condition: getConditions(state),
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
    permissions: getPermissions(state),
    brands: getBrands(state)
});

const mapDispatchToProps = dispatch => ({
    onChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onClickReset: () => dispatch(resetQueryPanel()),
    onSelect: data => dispatch(selectDealerQuery(data)),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
